<!--
 * @Author: WangShuai 1830773767@qq.com
 * @Date: 2024-01-14 20:42:28
 * @LastEditors: WangShuai 1830773767@qq.com
 * @LastEditTime: 2025-06-18 19:18:27
 * @FilePath: \vue3_admin_template\project\src\views\acl\permission\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-card>
    <el-table
      border
      :data="permissionList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      :expand-row-keys="[1]"
    >
      <el-table-column
        prop="name"
        label="名称"
        width="width"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="code"
        label="权限值"
        width="width"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="updateTime"
        label="修改时间"
        width="width"
        align="center"
      ></el-table-column>
      <el-table-column prop="prop" label="操作" width="width" align="center">
        <!-- row代表已有的菜单对象 -->
        <template #="{ row }">
          <el-button
            type="primary"
            :disabled="row.level == 4 ? true : false"
            size="small"
            title="添加菜单"
            icon="user"
            @click="addPermisstion(row)"
            v-has="`btn.Permission.add`"
          >
            {{ row.level == 3 ? '添加功能' : '添加菜单' }}
          </el-button>
          <el-button
            :disabled="row.level == 1 ? true : false"
            type="primary"
            size="small"
            title="编辑"
            icon="Edit"
            @click="updatePermisstion(row)"
            v-has="`btn.Permission.update`"
          >
            编辑
          </el-button>
          <el-popconfirm
            class="box-item"
            :title="`确定要删除${row.name}么?`"
            placement="top-start"
            @confirm="removePermission(row)"
            @cancel="removeCancel"
            width="200px"
          >
            <template #reference>
              <el-button
                :disabled="row.level == 1 ? true : false"
                type="primary"
                size="small"
                title="删除"
                icon="Delete"
                v-has="`btn.Permission.remove`"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框组件:添加或者更新已有菜单的数据结构 -->
    <el-dialog
      v-model="dialogVisible"
      :title="`${permissionParmas.id ? '更新' : '添加'}菜单`"
      width="500"
    >
      <el-form
        ref="formRef"
        label-width="80px"
        :model="permissionParmas"
        :rules="rules"
      >
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="permissionParmas.name"
            placeholder="请输入名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="权限" prop="code">
          <el-input
            v-model="permissionParmas.code"
            placeholder="请输入权限值"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button size="small" @click="cancle">取消</el-button>
          <el-button size="small" type="primary" @click="saveAddPermission">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup lang="ts">
import { onMounted, ref, reactive, nextTick } from 'vue'
import {
  reqAllPermission,
  reqRemoveAllPermission,
  addOrUpdateMenu,
} from '@/api/acl/menu'
import type {
  PermissionResponseData,
  Permission,
  MenuParams,
} from '@/api/acl/menu/type'
import { ElMessage } from 'element-plus'
let permissionList = ref<Permission[]>([])
onMounted(() => {
  getHasPermission()
})
let permissionParmas = reactive<MenuParams>({
  id: 0,
  name: '',
  code: '',
  pid: 0,
  level: 0,
})
//控制添加菜单对话框的显示和隐藏
let dialogVisible = ref<boolean>(false)
let formRef = ref()
const getHasPermission = async () => {
  let result: PermissionResponseData = await reqAllPermission()
  if (result.code == 200) {
    permissionList.value = result.data
  }
}
//@ts-ignore
const validatorName = (rule: any, value: any, callback: any) => {
  if (value.trim().length >= 2) {
    callback()
  } else {
    callback(new Error('菜单名称长度至少2位!'))
  }
}
//@ts-ignore
const validatorAcl = (rule: any, value: any, callback: any) => {
  if (value.trim().length >= 2) {
    callback()
  } else {
    callback(new Error('菜单权限值长度至少2位!'))
  }
}
//自定义表单校验规则
let rules = {
  name: [{ required: true, tigger: 'blur', validator: validatorName }],
  code: [{ required: true, tigger: 'blur', validator: validatorAcl }],
}
//添加菜单
const addPermisstion = async (row: Permission) => {
  Object.assign(permissionParmas, {
    id: 0,
    name: '',
    code: '',
    pid: 0,
    level: 0,
  })
  //收集菜单的level,pie属性
  permissionParmas.pid = row.id as number
  permissionParmas.level = row.level + 1
  dialogVisible.value = true
  nextTick(() => {
    formRef.value.clearValidate('name')
    formRef.value.clearValidate('code')
  })
}
//编辑已有菜单
const updatePermisstion = (row: Permission) => {
  Object.assign(permissionParmas, row)
  dialogVisible.value = true
  nextTick(() => {
    formRef.value.clearValidate('name')
    formRef.value.clearValidate('code')
  })
}
//删除菜单
const removePermission = async (row: Permission) => {
  Object.assign(permissionParmas, row)
  let result: PermissionResponseData = await reqRemoveAllPermission(
    permissionParmas.id as number,
  )
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除菜单成功!',
    })
    getHasPermission()
  } else {
    ElMessage({
      type: 'error',
      message: '删除菜单失败!',
    })
  }
}
//添加或者修改菜单方法
const saveAddPermission = async () => {
  await formRef.value.validate()
  let result: any = await addOrUpdateMenu(permissionParmas)
  if (result.code == 200) {
    dialogVisible.value = false
    ElMessage({
      type: 'success',
      message: `${permissionParmas.id ? '修改' : '添加'}菜单成功!`,
    })
    getHasPermission()
  } else {
    ElMessage({
      type: 'error',
      message: `${permissionParmas.id ? '修改' : '添加'}菜单失败!`,
    })
  }
}
//添加菜单取消方法
const cancle = () => {
  Object.assign(permissionParmas, {
    id: 0,
    name: '',
    code: '',
    pid: 0,
    level: 0,
  })
  nextTick(() => {
    formRef.value.clearValidate('name')
    formRef.value.clearValidate('code')
  })
  dialogVisible.value = false
}
const removeCancel = () => {
  ElMessage({
    type: 'success',
    message: `操作取消!`,
  })
}
</script>

<style lang="scss" scoped></style>
